package rmy.service.impl;

import onbon.bx06.Bx6GEnv;
import onbon.bx06.Bx6GScreenClient;
import onbon.bx06.Bx6GScreenProfile;
import onbon.bx06.area.TextCaptionBxArea;
import onbon.bx06.area.page.TextBxPage;
import onbon.bx06.file.ProgramBxFile;
import onbon.bx06.series.Bx6E;
import onbon.bx06.utils.DisplayStyleFactory;
import onbon.bx06.utils.TextBinary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rmy.domain.BerrArea;
import rmy.domain.BerrParam;
import rmy.service.BerrInterface;

import java.util.List;

public class OnbonBx06BerrDevice implements BerrInterface {

    private static final Logger logger = LoggerFactory.getLogger(OnbonBx06BerrDevice.class);

    @Override
    public boolean sendBerr(String strIp, int nPort, BerrParam compParam, List<BerrArea> berrAreaList) throws Exception {
        Bx6GEnv.initial(30000);
        Bx6GScreenClient screen = new Bx6GScreenClient("compScreen",new Bx6E());
        if (!screen.connect(strIp, nPort)) {
            throw new Exception("connect " + strIp + ":" + nPort + " failed!!!");
        }
        Bx6GScreenProfile profile = screen.getProfile();
        DisplayStyleFactory.DisplayStyle[] styles = DisplayStyleFactory.getStyles().toArray(new DisplayStyleFactory.DisplayStyle[0]);

        ProgramBxFile pf = new ProgramBxFile( "P888",screen.getProfile() );

        int nId = 0;
        // 1. 循环取值
        for (BerrArea berrArea : berrAreaList) {
            TextCaptionBxArea area = new TextCaptionBxArea(berrArea.getX(),berrArea.getY(),berrArea.getW(),berrArea.getH(), profile);
            TextBxPage page = new TextBxPage(berrArea.getContent());
            page.setDisplayStyle(styles[berrArea.getMode()]);
            page.setHorizontalAlignment(TextBinary.Alignment.CENTER);
            area.addPage(page);
            pf.addArea(area);
        }
        // 更新节目
        screen.writeProgram(pf);
        // 断开连接
        screen.disconnect();

        return true;
    }
}
